package 题目集.位运算.集合论;

import org.junit.Test;

/**
 * https://leetcode.cn/problems/find-the-k-or-of-an-array/description/
 */
public class demo01_100_找出数组中的K_or值 {

    public int findKOr(int[] nums, int k) {
        int[] bitmap = new int[31];
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; nums[i] > 0; j++, nums[i] = nums[i] >> 1) {
                bitmap[j] += nums[i] & 1;
            }
        }
        int res = 0;
        for (int i = bitmap.length - 1; i >= 0; i--) {
            res = res << 1;
            if (bitmap[i] >= k) {
                res = res | 1;
            }
        }
        return res;
    }

    @Test
    public void test() {
        int[] arr = {7, 12, 9, 8, 9, 15};
        System.out.println(findKOr(arr, 4));
    }
}
